<template>
    <div id="TurnTohold">
        <el-dialog title="转办" :visible.sync="dialogVisible" width="30%"  :before-close="submitFormfalse">
            <div>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="转办原因" prop="reason">
                        <el-input v-model="ruleForm.reason" type="textarea" autosize></el-input>
                    </el-form-item>
                    <el-form-item label="转给">
                        <el-radio v-model="radio1" label="1">转办人</el-radio>
                        <el-radio v-model="radio1" label="2" v-if="AuditDetails.current_level_id < AuditDetails.final_level_id">下一级</el-radio>
                    </el-form-item>
                    <el-form-item label="" v-if="radio1 == 1" prop="transfer_user_id">
                        <div>
                            <el-select v-model="ruleForm.transfer_user_id" filterable placeholder="请选择"
                                style="width: 100%;">
                                <el-option v-for="item in approverData" :label="item.name" :value="item.id + ''"
                                    :key="item.id + ''"  :disabled="item.disabled">{{ item.name }}</el-option>
                            </el-select>
                        </div>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="submitFormfalse()">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    import { userList } from "@/api/waybill";
    import {
        examine_record_transfer
    } from "@/api/paymentrequest";
    export default {
        props: ['audit_id_list',"AuditDetails"],
        data() {
            return {
                dialogVisible: true,
                radio1: "1",
                ruleForm: {
                    transfer_user_id: "",  // 转移用户id, 填则转给其它人，不填则转给下一级
                    reason: ""  // 转办原因必填
                },
                rules: {
                    reason: [
                        { required: true, message: '请输入转办原因', trigger: 'blur' },
                    ],
                    transfer_user_id: [
                        { required: true, message: '请选择转办人', trigger: 'blur' },
                    ],
                },
                approverData: [],
                datalist: {
                    resstatus: 0,//0 取消 1 是成功后取消
                },
                localgetdata:{},
            };
        },
        watch: {
            radio1(item1, item) {
                if (item1 == 2) {
                    this.ruleForm.transfer_user_id = ""
                }
                console.log(this.ruleForm.transfer_user_id, "this.ruleForm.transfer_user_id")
            }
        },
        mounted() {
            this.localgetdata = JSON.parse(localStorage.getItem("user"));
            userList().then((res) => {
                const data = res.data || [];
                this.approverData = data.map((item) => {
                    item.name = item.nickname;
                    if(item.id == this.localgetdata.user_id) {
                     return { ...item,  disabled: true}; 
                    }
                    return item;
                });
                console.log(this.approverData,"this.approverDatathis.approverData")
            });
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var param = {
                            ...this.ruleForm,
                            id: this.audit_id_list,  // 与通过打回所传id相同
                        }
                        examine_record_transfer(param).then((res) => {
                            if (res.code == 10000) {
                                this.datalist.resstatus = 1
                                this.$emit('listenToChildEvent', this.datalist)
                            } else {
                                this.meragesObj(res.message,"warning")
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            submitFormfalse() {
                this.datalist.resstatus = 0
                this.$emit('listenToChildEvent', this.datalist)
            },
            meragesObj(mes,ty) {
                this.$message({
                    showClose: true,
                    message: mes,
                    type: ty
                });
            }
        }
    };
</script>
<style lang="scss" scoped>

</style>